/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import matching.ClassMatch;
import matching.triple.ClassTriple;
import matching.DataPropertyMatch;
import matching.triple.DataPropertyTriple;
import matching.Match;
import matching.ObjectPropertyMatch;
import matching.triple.ObjectPropertyTriple;
import matching.triple.Triple;
import matching.xml.MatchingManager;
import org.semanticweb.owl.model.OWLDataProperty;

/**
 *
 * @author livia
 */
public class MatchingViewManager {

    protected class TripleViewManager {
        private JTextField valueRest;
        private JComboBox restView;
        private JTextArea matchView;
        private Triple triple;

        public TripleViewManager(JTextField valueRest, JComboBox restView, JTextArea matchView) {
            this.valueRest = valueRest;
            this.restView = restView;
            this.matchView = matchView;
            this.restView.removeAllItems();
            matchView.setEditable(false);
        }

        public void loadTripleViewer(ClassTriple t){
            triple = t;
            String out = "CLASS: "+t.getElement()+"\nCONTEXT: Class Top";
            matchView.setEditable(true);
            matchView.setText(out);
            matchView.setEditable(false);
            restView.addItem("");
            for (OWLDataProperty dt : t.onto.getDataPropertiesOf(t.getElement().toString()) ) {
                restView.addItem(dt);
            }
        }

        public void clear(){
            this.valueRest.setText(null);
            this.restView.removeAllItems();
            this.matchView.setText(null);
        }

        public void loadTripleViewer(DataPropertyTriple t){
            triple = t;
            String out = "DATA PROPERTY: "+t.getElement()+"\nCONTEXT: "+t.getContext();
            matchView.setEditable(true);
            matchView.setText(out);
            matchView.setEditable(false);
            selectedMatch = -1;
        }

        public void loadTripleViewer(ObjectPropertyTriple t){
            triple = t;
            String out = "OBJECT PROPERTY: "+t.getElement()+"\nCONTEXT: "+t.getContext();
            matchView.setEditable(true);
            matchView.setText(out);
            matchView.setEditable(false);
            selectedMatch = -1;
        }

        public void updateRestriction(){
            if(triple instanceof ClassTriple){
                System.out.println(restView.getSelectedItem());
                System.out.println( valueRest.getText());
               ((ClassTriple)triple).setRestriction(restView.getSelectedItem().toString() , valueRest.getText());
            }
        }
    }
    TripleViewManager srcView;
    TripleViewManager tgtView;
    private JTable matchingTable;
    private static MatchingViewManager instace;
    private MatchingManager matchingManager;
    private int selectedMatch;

    private MatchingViewManager() {
        selectedMatch = -1;
        matchingManager = MatchingManager.getInstance();
    }

    public void setSelectedMatch(int n){
        selectedMatch = n;
    }

    public static MatchingViewManager getInstance(){
        if(instace==null){
            instace = new MatchingViewManager();
        }
        return instace;
    }

    protected void setValues(JTextField valueRestSrc, JTextField valueRestTgt, JComboBox restSrcView, JComboBox restTgtView, JTextArea matchSrcView, JTextArea matchTgtView, JTable table) {
        srcView = new TripleViewManager(valueRestSrc, restSrcView, matchSrcView);
        tgtView = new TripleViewManager(valueRestTgt, restTgtView, matchTgtView);
        this.matchingTable = table;
    }

    public void loadTable(){
        matchingTable.setEnabled(true);
        for (Match m : MatchingManager.getInstance().matching.matchings){            
            addMatchInTable(m);
        }
        matchingTable.setEnabled(false);       
    }

    public void addMatchInTable(Match m){        
        DefaultTableModel model = (DefaultTableModel)matchingTable.getModel();
        matchingTable.setEnabled(true);
        model.addRow(m.toArray());
        matchingTable.setEnabled(false);
        srcView.clear();
        tgtView.clear();
    }

    public void editSelectedMatch(){
        matchingManager.matching.matchings.remove(selectedMatch);
        Match m = getMatch();
        matchingManager.matching.matchings.add(m);
        matchingTable.setEnabled(true);
        DefaultTableModel model = (DefaultTableModel)matchingTable.getModel();
        model.removeRow(selectedMatch);
        model.addRow(m.toArray());
        matchingTable.setEnabled(false);
        srcView.clear();
        tgtView.clear();
        selectedMatch = -1;
    }

    public void delSelectedMatch(){
        matchingManager.matching.matchings.remove(selectedMatch);
        matchingTable.setEnabled(true);
        DefaultTableModel model = (DefaultTableModel)matchingTable.getModel();
        model.removeRow(selectedMatch);
        matchingTable.setEnabled(false);
        srcView.clear();
        tgtView.clear();
        selectedMatch = -1;
    }

    public void checkValidMatch() {
        if(hasPossibleMatch() && selectedMatch==-1){
            if(srcView.triple.getClass() == tgtView.triple.getClass()){
                Match m = getMatch();
                MatchingManager.getInstance().addMatch(m);
                matchingTable.setEnabled(true);
                addMatchInTable(m);
                matchingTable.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(null, "The kinds of elements in the match are incompatible.\n"
                       + "The elements must have the same type.");
            }
        }
    }

    public boolean hasPossibleMatch(){
        return srcView.triple != null && tgtView.triple != null;
    }
    

    private Match getMatch(){
        srcView.updateRestriction();
        tgtView.updateRestriction();

        if(srcView.triple instanceof ClassTriple){
            return new ClassMatch((ClassTriple)srcView.triple, (ClassTriple)tgtView.triple, srcView.triple.getOnto(), tgtView.triple.getOnto());
        }else if(srcView.triple instanceof ObjectPropertyTriple){
            return new ObjectPropertyMatch((ObjectPropertyTriple)srcView.triple, (ObjectPropertyTriple)tgtView.triple, srcView.triple.getOnto(), tgtView.triple.getOnto());
        }else {
            return new DataPropertyMatch((DataPropertyTriple)srcView.triple, (DataPropertyTriple)tgtView.triple, srcView.triple.getOnto(), tgtView.triple.getOnto());
        }
    }

    public void save(){
        try {
            MatchingManager.getInstance().saveMatching();
        } catch (IOException ex) {
            Logger.getLogger(MatchingViewManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
